#include "elcomponent.h"
#include <QPainter>
#include "eltooltip.h"

ElComponent::ElComponent(QWidget *parent)
    : QWidget{parent}
{}

void ElComponent::setX(int x)
{
    move(x, y());
}

void ElComponent::setY(int y)
{
    move(x(), y);
}

void ElComponent::setPalette(const ElPalette &palette)
{
    _palette = palette;
    update();
}

ElPalette ElComponent::palette() const
{
    return _palette;
}

void ElComponent::setOpacity(qreal opacity)
{
    if (_opacity == opacity)
        return;
    _opacity = opacity;
    update();
    emit opacityChanged(_opacity);
}

qreal ElComponent::opacity() const
{
    return _opacity;
}

ElTooltip *ElComponent::tooltip() const
{
    return _tooltip;
}

void ElComponent::setTooltip(ElTooltip *tooltip)
{
    if (_tooltip == tooltip)
        return;
    delete _tooltip;
    _tooltip = tooltip;
    // _tooltip->setTarget(this);
}

void ElComponent::initPainter(QPainter *painter) const
{
    painter->setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing
                            | QPainter::SmoothPixmapTransform);
    painter->setOpacity(_opacity);
    painter->setFont(font());
}

void ElComponent::showTooltip()
{
    if (_tooltip == nullptr || _tooltip->isManual())
        return;
    _tooltip->show();
}

void ElComponent::hideTooltip()
{
    if (_tooltip == nullptr || _tooltip->isManual())
        return;
    _tooltip->fade();
}
